package pers.mingshan.thread.thread;

/**
 * 共享数据
 * @author mingshan
 *
 */
public class MultiThreadShareData {

    public static void main(String[] args) {
        /*ShareData1  s=new ShareData1();
        new Thread(s).start();
        new Thread(s).start();*/
        final ShareData1  s=new ShareData1();
        new Thread(new MyRunnable1(s)).start();
        new Thread(new MyRunnable1(s)).start();
    }
}

class ShareData1 implements Runnable {
    private int count = 100;

    @Override
    public void run() {
        while(count>0) {
            count--;
            System.out.println("Thread-Name" + Thread.currentThread().getName() + " " + count);
        }
    }

    private int j = 0;

    public synchronized void increment() {
        j++;
    }

    public  synchronized void decrement() {
        j--;
    }
}

class MyRunnable1 implements Runnable {
    private ShareData1 data1;

    public MyRunnable1(ShareData1 data1) {
        this.data1=data1;
    }

    @Override
    public void run() {
        data1.increment();
    }
}

class MyRunnable2 implements Runnable {
    private ShareData1 data1;

    public MyRunnable2(ShareData1 data1) {
        this.data1=data1;
    }

    @Override
    public void run() {
        data1.decrement();
    }
}